import {
  getCategoryList,
  getCategoryDetail
} from '../../../../services/category/index';
Page({
  data: {
    dataList: [],
    childList: [],
    pageNo: 1,
    pageSize: 10,
    currentActive: 0,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    categroykId: "",
    pullDownRefreshing: false,
    goodsListLoadStatus: 0
  },
  init() {
    getCategoryList({}).then((res) => {
      this.setData({
        dataList: res.data,
      });
      if (res.data.length > 0) {
        this.getChildList(res.data[0]);
      }
    });
  },
  getChild(e) {
    this.onReset();
    const {
      item,
      index
    } = e.target.dataset;
    this.setData({
      categroykId: item.kid
    })
    getCategoryDetail({
      param: {
        classifyId: item.kid,
        shelveFlag: 1,
      },
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    }).then((res) => {
      for (const item of res.data.entities) {
        if (item.picSmall) {
          item.picSmall = item.picSmall.split(',');
        }
      }
      this.setData({
        childList: res.data.entities,
        currentActive: index,
      });
    });
  },
  async loadMore() {
    console.log('freshmore');
    console.log(this.data.currentActive);
    if (this.data.goodsListLoadStatus === 0) {
      const pageSize = this.data.pageSize;
      let pageIndex = this.data.pageNo + 1;
      try {
        const {
          data
        } = await getCategoryDetail({
          "pageNo": pageIndex,
          "pageSize": pageSize,
          "param": {
            classifyId: this.data.dataList[this.data.currentActive].kid,
            shelveFlag: 1,
          }
        });
        for (const item of data.entities) {
          if (item.picSmall) {
            item.picSmall = item.picSmall.split(',');
          }
        }
        this.setData({
          childList: this.data.childList.concat(data.entities),
          goodsListLoadStatus: data.entities.length > 1 ? 0 : 2,
        });

        this.data.pageSize = data.pageSize;
        this.data.pageNo = data.pageNo;
      } catch (err) {
        console.log(err);
        this.setData({
          goodsListLoadStatus: 3
        });
      }
    }
  },

  getChildList(item) {
    this.onReset();
    getCategoryDetail({
      param: {
        classifyId: item.kid,
        shelveFlag: 1,
      },
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    }).then((res) => {
      for (const item of res.data.entities) {
        if (item.picSmall) {
          item.picSmall = item.picSmall.split(',');
        }
      }
      this.setData({
        childList: res.data.entities,
      });
    });
  },
  onReset() {
    this.setData({
      pageNo: 1,
      pageSize: 10,
      goodsListLoadStatus: 0
    });
  },
  onShow() {
    // this.init();
  },
  onChange() {
    wx.navigateTo({
      url: '/subpages/pages/goods/list/index',
    });
  },
  gotoDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/subpages/pages/goods/details/index?spuId=${id}`,
    });
  },
  onLoad() {
    this.init(true);
  },
});